ALTER TRIGGER tgr_RegisterCourse
ON takes
FOR INSERT
AS
	DECLARE @course_id AS VARCHAR(8);
	DECLARE @sec_id AS VARCHAR(8);
	DECLARE @semester AS VARCHAR(6);
	DECLARE @year AS NUMERIC(4, 0);
	SELECT @course_id=course_id, @sec_id=sec_id, @semester = semester, @year=year FROM INSERTED;
	
	
	IF
	((SELECT COUNT(ID) FROM takes WHERE course_id = @course_id AND sec_id = @sec_id AND semester = @semester AND year = @year)
	>= 
	(SELECT capacity FROM classroom WHERE
		building = (SELECT building FROM section WHERE course_id = @course_id AND sec_id = @sec_id AND semester = @semester AND year = @year) AND
		room_number = (SELECT room_number FROM section WHERE course_id = @course_id AND sec_id = @sec_id AND semester = @semester AND year = @year)))
	BEGIN
		PRINT 'Dang ky khong thanh cong! Phong hoc da day';
		ROLLBACK TRANSACTION;
	END
	